package com.demo.stream_;

import org.junit.Test;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author gy
 * @date 2022/5/10
 *  提取/组合
 * 流也可以进行合并、去重、限制、跳过等操作。
 * concat 流合并 distinct  去重 ,两者组合使用
 * limit 限制从流中获取 前 n 个数据
 * skip 跳过前n个数据
 */
public class StreamConcatLimitSkip {
    @Test
    public void m1(){
        String[] arr1 = { "a", "b", "c", "d" };
        String[] arr2 = { "d", "e", "f", "g" };

        Stream<String> stream1 = Stream.of(arr1);
        Stream<String> stream2 = Stream.of(arr2);
        // concat:合并两个流 distinct：去重
        List<String> newList = Stream.concat(stream1,stream2)
                .distinct()
                .collect(Collectors.toList());
        // limit：限制从流中获得前n个数据
        List<Integer> collect = Stream.iterate(100, x -> x + 2).
                limit(10).collect(Collectors.toList());
        // skip：跳过前n个数据
        List<Integer> collect2 = Stream.iterate(1, x -> x + 2)
                .skip(3).limit(7).collect(Collectors.toList());

        System.out.println("流合并：" + newList);
        System.out.println("limit：" + collect);
        System.out.println("skip：" + collect2);
    }
}
